﻿#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

// 枚举值表⽰颜⾊
enum Colour
{
	RED,
	BLACK
};


template <class K, class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{
	}
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:

	void rotateR(Node* parent) //右单旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR) //防止对空指针解引用
			subLR->_parent = parent;

		Node* pParent = parent->_parent; //先记录旋转前parent的父节点

		subL->_right = parent;
		parent->_parent = subL;
		if (pParent == nullptr) //旋转前parent为根节点
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else //旋转前parent不为根节点
		{
			subL->_parent = pParent;
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}
		}
	}

	void rotateL(Node* parent) //左旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* pParent = parent->_parent; //记录parent的_parent
		subR->_left = parent;
		parent->_parent = subR;

		if (pParent == nullptr)//更新前的parent是整棵树根节点
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			subR->_parent = pParent;

			if (pParent->_left == parent)
				pParent->_left = subR;
			else
				pParent->_right = subR;
		}
	}

	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;  //根节点为黑色
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else return false;
		}
		cur = new Node(kv);
		cur->_col = RED;  //新插入节点为红色
		if (cur->_kv.first < parent->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left) //u在右
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED) //u存在且为红
				{
					parent->_col = BLACK; //u和p变黑
					uncle->_col = BLACK;
					grandfather->_col = RED;//g变红

					cur = grandfather; //继续向上更新
					parent = cur->_parent;
				}
				else //u不存在 或 u存在且为黑
				{
					if (cur == parent->_left) //单旋
					{
						rotateR(grandfather);//以g为旋转点右旋
						parent->_col = BLACK;  //变色
						grandfather->_col = RED;
					}
					else //双旋
					{
						rotateL(parent); //先对p左旋
						rotateR(grandfather);//再对g右旋
						//变色
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else //u在左
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED) //u存在且为红
				{
					parent->_col = BLACK; //p和u变黑
					uncle->_col = BLACK;
					grandfather->_col = RED;//g变红

					cur = grandfather; //继续向上更新
					parent = cur->_parent;
				}
				else //u不存在 或 存在且为黑
				{
					if (cur == parent->_right) //单旋
					{
						rotateL(grandfather);//以g为中心左旋
						parent->_col = BLACK; //p变黑
						grandfather->_col = RED;//g变红
					}
					else //双旋
					{
						rotateR(parent);//先以p为中心右旋
						rotateL(grandfather);//再以g为中心左旋
						cur->_col = BLACK; //c变黑
						grandfather->_col = RED;//g变红
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}


	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;
		// 参考值
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}

	void Inoder() //中序遍历
	{
		_Inorder(_root);
		cout << endl;
	}

private:
	void _Inorder(const Node* root)
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << ' ';
		_Inorder(root->_right);
	}

	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历走到空时，意味着⼀条路径走完了
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在黑色结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		// 检查孩子不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就方便多了
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红色结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}

	Node* _root = nullptr;

};